import sys
import time
from SeerGlobal import *
task = get_value('Task')

from PyQt5.QtCore import *#Qt
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
_path = ''
_path = sys.argv[0]
_path = os.path.dirname(_path)
if _path[-8:] == 'SeerMain':
    _path = str(_path[0:-8])
_path2 = _path + '\\List\\0043'
_path3 = _path2 + '\\精灵配置'
sys.path.append(_path + '\\List\\0043')
from py0043 import Ui_MainWindow

class jobWindows(Ui_MainWindow,QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.data = []
        self.treeWidget.itemDoubleClicked.connect(self.setFlag)
        self.treeWidget.setContextMenuPolicy(Qt.CustomContextMenu)#打开右键菜单的策略
        self.treeWidget.customContextMenuRequested.connect(self.setMenu)#绑定事件
        self.pushButton.clicked.connect(self.addData)
        self.comboBox.currentIndexChanged.connect(self.switch)#切换困难简单模式
        self.comboBox_2.currentIndexChanged.connect(self.switch2)#切换挑战条件
        self.pushButton_2.clicked.connect(self.pickle_jobCun)
        s = []
        for i in range(10):
            i = []
            s.append(list(i))
        self.data.append(list(s))
        s = []
        for i in range(10):
            i = []
            s.append(list(i))
        self.data.append(list(s))
        s = []
        for i in range(10):
            i = []
            s.append(list(i))
        self.data.append(list(s))
        
        try:
            data01 = self.pickle_jobQu()
            if(data01 != None):
                self.data = list(data01)
        except:
            print("没有在0043文件夹内发现精灵配置文件")
        
        #此处应该更新完毕数据了
        list01 = self.data
        a1 = self.comboBox.currentIndex()
        a2 = self.comboBox_2.currentIndex()
        list02 = list01[a1][a2]#获取到当前的数组了
        self.updateUI(list02)
    
    def closeEvent(self, event):
        super().closeEvent(event)
        print("窗口已关闭")
        global isOpenWin
        isOpenWin = 0
    
    def getTreeWidgetData(self):
        #获取树状图数据表的数据
        data01 = []
        data02 = []
        int01 = self.treeWidget.topLevelItemCount()#总数量
        for i in range(int01):
            Item = self.treeWidget.topLevelItem(i)
            data02.append(Item.text(0))
            data02.append(Item.text(1))
            data01.append(list(data02))
            data02.clear()
        return data01
    
    def setFlag(self, item: QTreeWidgetItem, column: int):
        #树状图双击绑定事件，需要编辑这一行
        #column为0是精灵，为1是技能
        #Qt.ItemIsDragEnabled | Qt.ItemIsEnabled
        item.setFlags(Qt.ItemIsEditable | Qt.ItemIsEnabled)#编辑这一行
    
    def setMenu(self, pos: QPoint):
        #鼠标右键菜单
        try:
            self.contextMenu = QMenu()#创建对象
            self.actionA = self.contextMenu.addAction(u'上移')#添加动作
            self.actionB = self.contextMenu.addAction(u'下移')
            self.actionC = self.contextMenu.addAction(u'删除')
            self.actionA.triggered.connect(self.actionHandlerA)
            self.actionB.triggered.connect(self.actionHandlerB)
            self.actionC.triggered.connect(self.actionHandlerC)
            self.contextMenu.exec_(self.treeWidget.mapToGlobal(pos))#随指针的位置显示菜单
            self.contextMenu.show()#显示
        except Exception as e:
            print(e)
    
    def actionHandlerA(self):#上移
        self.contextMenu.clear()
        Item = self.treeWidget.currentItem()
        rootIndex = self.treeWidget.indexOfTopLevelItem(Item)
        Item2 = self.treeWidget.topLevelItem(rootIndex - 1)
        if(Item2 != None):
            Item2_0 = Item2.text(0)#str
            Item2_1 = Item2.text(1)#str
            Item2.setText(0,Item.text(0))
            Item2.setText(1,Item.text(1))
            Item.setText(0,Item2_0)
            Item.setText(1,Item2_1)
        
        int01 = self.comboBox.currentIndex()
        int02 = self.comboBox_2.currentIndex()
        self.data[int01][int02] = self.getTreeWidgetData()
        
    def actionHandlerB(self):#下移
        self.contextMenu.clear()
        Item = self.treeWidget.currentItem()
        rootIndex = self.treeWidget.indexOfTopLevelItem(Item)
        Item2 = self.treeWidget.topLevelItem(rootIndex + 1)
        if(Item2 != None):
            Item2_0 = Item2.text(0)#str
            Item2_1 = Item2.text(1)#str
            Item2.setText(0,Item.text(0))
            Item2.setText(1,Item.text(1))
            Item.setText(0,Item2_0)
            Item.setText(1,Item2_1)
        int01 = self.comboBox.currentIndex()
        int02 = self.comboBox_2.currentIndex()
        self.data[int01][int02] = self.getTreeWidgetData()
        
    def actionHandlerC(self):#删除
        self.contextMenu.clear()
        #print(self.treeWidget.currentItem().text(0))
        Item = self.treeWidget.currentItem()
        rootIndex = self.treeWidget.indexOfTopLevelItem(Item)
        self.treeWidget.takeTopLevelItem(rootIndex)
        
        int01 = self.comboBox.currentIndex()
        int02 = self.comboBox_2.currentIndex()
        try:
            del self.data[int01][int02][rootIndex]
        except:
            print("删除了不存在的列表元素")
    
    def addData(self, checked: bool):
        #点击添加--把文本添加到出招表
        _translate = QCoreApplication.translate
        jing = self.lineEdit_2.text()#精灵
        ji = self.lineEdit.text()#技能
        if(jing != '' and ji != ''):
            Item = QTreeWidgetItem(self.treeWidget)
            rootIndex = self.treeWidget.indexOfTopLevelItem(Item)
            self.treeWidget.topLevelItem(rootIndex).setText(0, _translate("MainWindow", jing))
            self.treeWidget.topLevelItem(rootIndex).setText(1, _translate("MainWindow", ji))
            i = []
            i.append(jing)
            i.append(ji)
        
            int01 = self.comboBox.currentIndex()
            int02 = self.comboBox_2.currentIndex()
            print('模式：' + str(int01))
            print('条件：' + str(int02))
            self.data[int01][int02].append(i)
    
    def updateUI(self,list01):
        #根据当前的数组数据，恢复树状图里面的出招表
        self.treeWidget.clear()#清空当前
        _translate = QCoreApplication.translate
        for i in list01:#[['帝皇之御', '守御八方'], ['艾欧丽娅', '有女初长成'], ['时空界皇', '第五']]
            if(len(i) == 2):
                #['帝皇之御', '守御八方']
                Item = QTreeWidgetItem(self.treeWidget)
                rootIndex = self.treeWidget.indexOfTopLevelItem(Item)
                self.treeWidget.topLevelItem(rootIndex).setText(0, _translate("MainWindow", i[0]))
                self.treeWidget.topLevelItem(rootIndex).setText(1, _translate("MainWindow", i[1]))
            else:
                print("报错：出招表数组格式不正确")
                print(i)
    
    def switch(self, a0: str):
        #切换六界的难度，以及挑战条件
        list01 = self.data
        a1 = self.comboBox_2.currentIndex()
        list02 = list01[a0][a1]#获取到当前的数组了
        self.updateUI(list02)
        #print(self.data)
        
    def switch2(self, a0: str):
        #切换六界的难度，以及挑战条件
        list01 = self.data
        a1 = self.comboBox.currentIndex()
        list02 = list01[a1][a0]#获取到当前的数组了
        self.updateUI(list02)
        #print(self.data)
    
    def pickle_jobCun(self):
        #序列化保存数组
        int01 = self.comboBox.currentIndex()
        int02 = self.comboBox_2.currentIndex()
        self.data[int01][int02] = self.getTreeWidgetData()#先保存当前的
        
        pickle_cun(_path3,self.data)
    
    def pickle_jobQu(self):
        #序列化读取文件
        return pickle_qu(_path3)
    
    def getData(self,a2):
        #获取当前条件的出招数据，通过self.data
        #a2就是条件编号，整数型
        a1 = self.comboBox.currentIndex()
        #[['帝皇之御', '守御八方'], ['艾欧丽娅', '有女初长成'], ['时空界皇', '第五']]
        list01 = []
        list02 = []
        list03 = []
        for i in self.data[a1][a2]:
            str01 = i[1]
            if(str01.find(',') != -1):
                list01 = str01.split(',')
            if(str01.find('，') != -1):
                list01 = str01.split('，')
            if(str01.find('，') == -1 and str01.find(',') == -1):#没有发现逗号
                list01 = []
                list01.append(str01)
            list02.append(i[0])
            list02.append(list(list01))
            list01 = []
            list03.append(list(list02))
            list02.clear()
        return list03

#task.addTask('0041')#开启战斗模块
isOpenWin = 1#如果窗口关闭的话就停止运行此脚本

jobw=jobWindows()
jobw.setWindowFlags(Qt.WindowStaysOnTopHint)#置顶要在show前面
jobw.show()

def job():
    #小帅猫1.6.7流畅画质--六界神王殿
    if(isOpenWin == 0):
        task.stopTask('0043')
        return
    if(Fun("0043","0043_1") == 1):#当前处于六界神王殿主界面
        int01 = jobw.comboBox.currentIndex()#当前界面的模式0,1,2
        if(int01 == 0):
            #Fun("0043","0043_2")#点击简单模式
            if(Fun("0043","0043_5") == 1):
                Fun("0043","0043_6")
        elif(int01 == 1):
            #Fun("0043","0043_3")#点击中等模式
            if(Fun("0043","0043_7") == 1):
                Fun("0043","0043_8")
        elif(int01 == 2):
            #Fun("0043","0043_4")#点击困难模式
            if(Fun("0043","0043_9") == 1):
                Fun("0043","0043_10")
    
    if(Fun("0043","0043_11") == 1):#每日可以进入1次关卡---弹窗点确认
        Fun("0043","0043_12")
    
    if(Fun("0043","0043_13") == 1):#活动说明--弹窗点确认
        Fun("0043","0043_14")
    
    if(Fun("0043","0043_23") == 1):#恭喜你按照要求完成认可---弹窗确认
        Fun("0043","0043_24")
    
    if(Fun("0043","0043_25") == 1):#界王印记放入背包弹窗--弹窗点确认
        Fun("0043","0043_26")
    
    if(Fun("0043","0043_36") == 1):#没打过弹窗
        Fun("0043","0043_37")
    
    if(Fun("0043","0043_38") == 1):#三回合以内战胜0
        出招表 = jobw.getData(0)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_15") == 1):#6回合以后战胜
        出招表 = jobw.getData(1)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_21") == 1):#使用致命一击战胜
        出招表 = jobw.getData(2)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_16") == 1):#能力提升状态战胜
        出招表 = jobw.getData(3)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_34") == 1):#先手下战胜4
        出招表 = jobw.getData(4)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_17") == 1):#特殊攻击战胜
        出招表 = jobw.getData(6)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_19") == 1):#≤3个精灵战胜
        出招表 = jobw.getData(5)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_20") == 1):#使用必中攻击战胜
        出招表 = jobw.getData(7)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_18") == 1):#物理攻击战胜
        出招表 = jobw.getData(8)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    if(Fun("0043","0043_35") == 1):#满体力战胜9
        出招表 = jobw.getData(9)
        task.addTask("ZhanDou","0043",出招表)
        Fun("0043","0043_22")#点击认证
    
    if(Fun("0043","0043_27") == 1):#开启认证为灰色
        if(Fun("0043","0043_28",0) == 1):
            pass
        if(Fun("0043","0043_29",0) == 1):
            pass
        if(Fun("0043","0043_30",0) == 1):
            pass
        if(Fun("0043","0043_31",0) == 1):
            pass
        if(Fun("0043","0043_32",0) == 1):
            pass
        if(Fun("0043","0043_33",0) == 1):
            pass